package com.czn.controller.admin;

import com.czn.entity.Doctor;
import com.czn.service.IDoctorService;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员对医生管理的控制器
 *
 * @author ChenZeNan
 * @create 2021-04-15 19:13
 */
@Controller
@RequestMapping("/admin/doctor")
public class AdminDoctorController {

    @Autowired
    private IDoctorService doctorService;

    /**
     * 跳转到医生管理页面
     *
     * @return
     */
    @RequestMapping("/manage")
    public String doctorManage(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                               @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                               @RequestParam(required = false) String name,
                               @RequestParam(required = false) String certId,
                               Model model) {
        PageQuery<Doctor> page = doctorService.findDoctorList(pageNo, pageSize, name, certId);
        model.addAttribute("page", page);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("name", name);
        model.addAttribute("certId", certId);
        model.addAttribute("path", "/admin/doctor/manage");
        model.addAttribute("params", "");
        if (name != null) {
            model.addAttribute("params", "&name=" + name);
        }
        return "admin/doctorManage";
    }

    /**
     * 跳转到新增医生页面
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String toAddFormPage(Model model) {
        model.addAttribute("doctor", new Doctor());
        return "admin/doctorForm";
    }

    /**
     * 跳转到医生修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String toEditFormPage(@PathVariable Integer id, Model model) {
        model.addAttribute("doctor", doctorService.findDoctorById(id));
        return "admin/doctorForm";
    }

    /**
     * 新增医生
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult add(@RequestBody Doctor doctor) {
        ResponseResult result = doctorService.addDoctor(doctor);
        return result;
    }

    /**
     * 编辑医生
     *
     * @param doctor
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseResult edit(@RequestBody Doctor doctor) {
        ResponseResult result = doctorService.editDoctor(doctor);
        return result;
    }

    /**
     * 删除医生
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseResult delete(@PathVariable Integer id) {
        ResponseResult result = doctorService.deleteDoctorById(id);
        return result;
    }

    /**
     * 根据科室查询医生
     *
     * @param department
     * @return
     */
    @RequestMapping("/getList/{department}")
    @ResponseBody
    public List<Doctor> getList(@PathVariable String department) {
        List<Doctor> doctors = doctorService.findDoctorListByDepartment(department);
        return doctors;
    }
}
